//
//  MyAppriseListViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 1/26/16.
//  Copyright © 2016 ShenOu. All rights reserved.
//

#import "MyAppriseListViewController.h"
#import "HOUserInfoEntity.h"
#import "HOAppraiserTableViewCell.h"
#import "HOAppraiseEntity.h"
#import "HOConstants.h"
#import "RatingBar.h"
#import "HOUIMgr.h"
#import "HOStaticDataMgr.h"

@interface MyAppriseListViewController ()<HOAppraiserTableViewCellDelegate>
{
	NSMutableArray *appraiseList;
	NSMutableDictionary *cellDic;
}
@property(nonatomic, retain)HOUserInfoEntity *userInfo;

@end

@implementation MyAppriseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	appraiseList = [[NSMutableArray alloc]init];
	cellDic = [[NSMutableDictionary alloc] init];
	[self initNoDataView];
	[self initRequestAgainView];
}

- (void)viewWillAppear:(BOOL)animated
{
	[self.navigationController.navigationBar setHidden:NO];
	[self.tabBarController.tabBar setHidden:YES];
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"我收到的评价", @"我收到的评价")];
	[baseTableview reloadData];
	self.userInfo = [[HOStaticDataMgr sharedInstance] getUserInfo];
	[self getMyAppraiseWithMaxId:0];
}

- (void)sendComplaint:(int)appraiseId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_Report,
										kHO_FIELD_AppraiseId:[NSNumber numberWithInt:appraiseId]}];
}

- (void)getMyAppraiseWithMaxId:(int)maxId
{
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetAppraiseList,
										kHO_FIELD_MaxID:[NSNumber numberWithInt:maxId],
										kHO_FIELD_Id:[NSNumber numberWithInt:self.userInfo.userId]}];
}

- (void)pullUpToRefresh
{
	HOAppraiseEntity *entity = [appraiseList lastObject];
	[self getMyAppraiseWithMaxId:entity.appraiseId];
}

- (void)pullDownToRefresh
{
	[self getMyAppraiseWithMaxId:0];
}

- (NSInteger)getListCount
{
	return [appraiseList count];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
	[appraiseList removeAllObjects];
	[appraiseList release];
	[cellDic removeAllObjects];
	[cellDic release];
	[super dealloc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark

- (HOAppraiserTableViewCell *)getCellWithAppraise:(HOAppraiseEntity *)entity
{
	if(entity == nil)
		return [[[HOAppraiserTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: @"HOAppraiserTableViewCell"] autorelease];
	HOAppraiserTableViewCell *cell =[cellDic objectForKey:[NSNumber numberWithInt:entity.appraiseId]];
	if(cell == nil)
	{
		
		cell = [[[HOAppraiserTableViewCell alloc]
				 initWithStyle:UITableViewCellStyleDefault
				 reuseIdentifier: @"HOAppraiserTableViewCell"] autorelease];
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		[cell initWithWidth:baseTableview.frame.size.width];
		[cell setEntity:entity];
		[cellDic setObject:cell forKey:[NSNumber numberWithInt:entity.appraiseId]];
	}
	return cell;

}

#pragma mark -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOAppraiseEntity *appraiseEntity = [appraiseList objectAtIndex:indexPath.row];
	HOAppraiserTableViewCell *cell = [self getCellWithAppraise:appraiseEntity];
	return [cell getCellHeight];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	NSInteger count = [appraiseList count];
	[self updateNoResultView:count];
	return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	UIView *view = [[[UIView alloc]
					 initWithFrame:CGRectMake(0,
											  0,
											  tableView.frame.size.width,
											  [self tableView:tableView heightForHeaderInSection:section])] autorelease];
	
	UILabel *headerLbl = [self getHeaderLblWithTitle: NSLocalizedString(@"综合评价", @"综合评价")];
	headerLbl.frame = CGRectMake(0,
								 (view.frame.size.height - 22) / 2,
								 tableView.frame.size.width / 2 - 5,
								 22);
	headerLbl.textColor = MAIN_COLOR_NORMAL;
//	headerLbl.layer.borderWidth = 1;
	headerLbl.font = [UIFont systemFontOfSize:14];
	[view addSubview:headerLbl];
	
	RatingBar *ratingBar = [[RatingBar alloc]
							initWithFrame:CGRectMake(headerLbl.frame.size.width + headerLbl.frame.origin.x + HO_HORIZONAL_PADDING,
																	headerLbl.frame.origin.y,
																	90,
																	8)  isSmall:NO];
//	ratingBar.layer.borderWidth = 1;
	ratingBar.starNumber = ((int)(self.userInfo.appraise + 1));
	[view addSubview:ratingBar];
	ratingBar.frame = CGRectMake(ratingBar.frame.origin.x,
								 (view.frame.size.height - ratingBar.frame.size.height) / 2,
								 ratingBar.frame.size.width,
								 ratingBar.frame.size.height);
	[ratingBar release];
	
	return view;
}

- (UILabel *)getHeaderLblWithTitle:(NSString *)title
{
	UILabel *headerLbl = [[[UILabel alloc]
						   initWithFrame:CGRectMake(0,
													50 / 2,
													self.view.frame.size.width / 2,
													22)] autorelease];
	headerLbl.textAlignment = NSTextAlignmentRight;
	headerLbl.text = title;
	headerLbl.textColor = MAIN_COLOR_NORMAL;
	return headerLbl;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	HOAppraiseEntity *appraiseEntity = [appraiseList objectAtIndex:indexPath.row];
	HOAppraiserTableViewCell *cell = [self getCellWithAppraise:appraiseEntity];
	return cell;
}
#pragma mark
- (HOAppraiseEntity *)getAppriaiseEntityWithId:(int)Id
{
	HOAppraiseEntity *entity = [entityDic objectForKey:[NSNumber numberWithInt:Id]];
	if(entity == nil)
	{
		entity = [[HOAppraiseEntity alloc]init];
		entity.appraiseId = Id;
		[entityDic setObject:entity forKey:[NSNumber numberWithInt:Id]];
		[entity release];
	}
	return entity;
}

#pragma mark
- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if(![response.action isEqualToString:kHO_ACTION_GetAppraiseList])
	{
		return;
	}
	[super didReceiveResponse:response];
	if([response isRequestDone])
	{
		if([response.action isEqualToString:kHO_ACTION_GetAppraiseList])
		{
			int maxId = [[[response.baseRequest params] objectForKey:kHO_FIELD_MaxID] intValue];
			if(maxId == 0)
			{
				[appraiseList removeAllObjects];
			}
			NSArray *appraises = [response.dataNode objectForKey:kHO_FIELD_AppraiseList];
			for(int i = 0; i < [appraises count]; i ++)
			{
				NSDictionary *dic = [appraises objectAtIndex:i];
				int Id = [[dic objectForKey:kHO_FIELD_AppraiseId] intValue];
				HOAppraiseEntity *entity = [self getAppriaiseEntityWithId:Id];
				entity.realName = [dic objectForKey:kHO_FIELD_RealName];
				entity.time = [[dic objectForKey:kHO_FIELD_Time] doubleValue];
				entity.value = [[dic objectForKey:kHO_FIELD_Value] intValue];
				entity.content = [dic objectForKey:kHO_FIELD_Content];
//				entity.iconURL = [dic objectForKey:kHO_FIELD_UserIconUrl];
				if(![appraiseList containsObject:entity])
					[appraiseList addObject:entity];
			}
			[self doneLoadingTableViewRefresh];
		}
	}
}
@end
